<template>
  <modal ref="modal_1"
         :title="title"
         :isCloseConfirm="false"
         top="5%"
         width="50%"
         height="50%"
         :close-on-click-modal="false"
         @modalOpened="opened"
         @modalClose="close">
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               v-model:page="page"
               v-model:search="search"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template #menu-left>
        <el-button
                type="warning"
                icon="Stamp"
                @click="handleAudit"
        >审 核
        </el-button>
      </template>
      <template #approveStatus="{ row }">
        <el-tag :type="row.approveStatus =='未审核' ? 'danger' : 'success'">{{ row.approveStatus }}</el-tag>
      </template>
      <template #menu="{ row, size, type }">
        <el-tooltip content="审核" placement="top">
          <el-button
                  icon="Stamp"
                  link
                  :size="size"
                  :type="type"
                  @click="() => rowAudit(row)"
                  v-if="row.approveStatus !== 'Approved'"
          >
          </el-button>
        </el-tooltip>
      </template>
    </avue-crud>
  </modal>
</template>

<script>
  import {getList, getDetail, add, update, remove,audit} from "@/api/anwise/customer/customer_material";
  import {mapGetters} from "vuex";


  export default {

    data() {
      return {
          customerId:'',
          title:'客户产品审核',
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 20,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
            height:'450px',
            calcHeight: 30,
            tip: false,
            searchShow: false,
            searchMenuSpan: 6,
            searchSpan:8,
            border: true,
            index: true,
            selection: true,
            searchLabelWidth:80,
            indexWidth:100,
            labelWidth: 150,
            menuWidth: 120,
            addBtn: false,
            editBtn: false,
            viewBtn: false,
            delBtn: false,
            reserveSelection:true,
            stripe: true,
            searchIndex: 3,
            searchIcon: false,
          searchEnter:true,
            menu:true,
            selectable:(row,index)=>{
                return !(row.approveStatus=='Approved');
            },
          column: [
              {
                  label: "物料编号",
                  prop: "materialNo",
                  type: "input",
                  search: true,
                  width:120,
                  overHidden: true,
                  sortable: true,
                  fixed:true,
                  rules: [{
                      required: true,
                      message: "请输入物料编号",
                      trigger: "blur"
                  }],
              },
              {
                  label: "客户条码",
                  prop: "barCode",
                  type: "input",
                  search: true,
                  overHidden: true,
                  sortable: true,
                  width:120,
              },
              {
                  label: "物料名称",
                  prop: "materialName",
                  type: "input",
                  search: true,
                  width:120,
                  overHidden: true,
                  sortable: true,
                  rules: [{
                      required: true,
                      message: "请输入物料名称",
                      trigger: "blur"
                  }],
              },
              {
                  label: "单价/100",
                  prop: "price",
                  type: "input",
                  search: true,
                  width:120,
                  overHidden: true,
                  sortable: true,
                  rules: [{
                      required: true,
                      message: "请输入单价/100",
                      trigger: "blur"
                  }],
              },
              {
                  label: "币别",
                  prop: "rmb",
                  type: "select",
                  filterable: true,
                  // search: true,
                  overHidden: true,
                  sortable: true,
                  width:120,
                  dicUrl: '/api/blade-system/dict-biz/dictionary?code=rmb',
                  props: {
                      label: 'dictValue',
                      value: 'dictKey',
                  },
                  dataType: 'string',
                  rules: [{
                      required: true,
                      message: "请选择币别",
                      trigger: "blur"
                  }],
              },
              {
                  label: "审批状态",
                  prop: "approveStatus",
                  type: "select",
                  dicUrl: "/blade-system/dict-biz/dictionary?code=approve_status",
                  filterable: true,
                  overHidden: true,
                  sortable: true,
                  width:150,
                  props: {
                      label: 'dictValue',
                      value: 'dictKey',
                  },
                  dataType: 'string',
                  rules: [{
                      required: true,
                      message: "请输入审批状态",
                      trigger: "blur"
                  }],
              },
              {
                  label: "类别",
                  prop: "materialType",
                  type: "select",
                  filterable:true,
                  search: true,
                  overHidden: true,
                  sortable: true,
                  width:120,
                  dicUrl: "/blade-system/dict-biz/dictionary?code=material_s_type",
                  width:120,
                  props: {
                      label: 'dictValue',
                      value: 'dictKey',
                  },
                  dataType: 'string',
                  rules: [{
                      required: true,
                      message: "请选择类别",
                      trigger: "blur"
                  }],
              },
              {
                  label: "海关编码",
                  prop: "mateGroup",
                  type: "input",
                  rules: [{
                      required: false,
                      message: "请输入海关编码",
                      trigger: "blur"
                  }],
                  search: true,
                  overHidden: true,
                  sortable: true,
                  width:120,
              },
              {
                  label: "系列代码",
                  prop: "seriesNo",
                  type: "select",
                  filterable:true,
                  dicUrl: '/api/blade-anwise/material_series/select',
                  props: {
                      label: 'seriesNo',
                      value: 'id',
                  },
                  dataType: 'string',
                  rules: [{
                      required: false,
                      message: "请选择系列代码",
                      trigger: "blur"
                  }],
                  search: true,
                  overHidden: true,
                  sortable: true,
                  width:120,
              },
              {
                  label: "品牌代号",
                  prop: "programCode",
                  type:"select",
                  filterable:true,
                  dicUrl: '/api/blade-anwise/customer_group/select',
                  props: {
                      label: 'programCode',
                      value: 'programCode',
                  },
                  dataType: 'string',
                  rules: [{
                      required: false,
                      message: "请选择品牌代号",
                      trigger: "blur"
                  }],
                  search: true,
                  overHidden: true,
                  sortable: true,
                  width:120,
              },
            {
              label: "审批人",
              prop: "approveName",
              type: "input",
                overHidden: true,
                sortable: true,
                width:120,
            },
            {
              label: "审批时间",
              prop: "approveDate",
              type: "input",
                overHidden: true,
                sortable: true,
                width:120,
            },
            {
              label: "上架人员",
              prop: "operName",
              type: "input",
                overHidden: true,
                sortable: true,
                width:120,
            },
            {
              label: "上架时间",
              prop: "operDate",
              type: "input",
                overHidden: true,
                sortable: true,
                width:120,
            },
            {
              label: "停用标识",
              prop: "stopComm",
                type: "select",
                dicUrl: "/blade-system/dict-biz/dictionary?code=biz_yes_no",
                filterable: true,
                overHidden: true,
                sortable: true,
                width:150,
                props: {
                    label: 'dictValue',
                    value: 'dictKey',
                },
                dataType: 'string',
              rules: [{
                required: true,
                message: "请输入停用标识",
                trigger: "blur"
              }],
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.validData(this.permission.customer_material_add, false),
          viewBtn: this.validData(this.permission.customer_material_view, false),
          delBtn: this.validData(this.permission.customer_material_delete, false),
          editBtn: this.validData(this.permission.customer_material_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
        show(customerId) {
            this.customerId = customerId;
          this.$refs.modal_1.show();
        },
        rowAudit(row) {
            this.loading = true;
            audit(row.id).then(res => {
                this.loading = false;
                if (res.data.success) {
                    this.onLoad(this.page);
                    this.$message({
                        type: "success",
                        message: res.data.msg || "操作成功!"
                    });
                }
            })
        },
        handleAudit() {
            if (this.selectionList.length === 0) {
                this.$message.warning("请选择至少一条数据");
                return;
            }
            this.loading = true;
            audit(this.ids).then(res => {
                this.loading = false;
                if (res.data.success) {
                    this.onLoad(this.page);
                    this.$message({
                        type: "success",
                        message: res.data.msg || "操作成功!"
                    });
                }
            })
        },
        opened() {
            //  this.onLoad(this.page);
        },
        close() {
            this.clearData();
            this.clearSelection();
        },

        handleAdd() {

            this.$refs.selectMaterial.showSelect(this.data);
        },
        setMaterialList(list) {
            console.log("this.data",this.data);
            let selectData = markRaw(list);
            console.log(selectData)
            selectData.forEach(item =>{
               if (!this.data.some((val) =>(val.materialId == item.id))) {
                   let mat = {
                       id:'',
                       materialId: item.id,
                       materialNo: item.materialNo,
                       materialName: item.materialName,
                   }
                   this.data.push(mat);
               }
            });
            // this.data = markRaw(list)
            // this.data.forEach(item =>{
            //     item.materialId = item.id;
            //     item.id = '';
            // })
        },
      setData(id) {
        this.customerId = id;
        console.log(id);
          this.onLoad(this.page)
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
            console.log(params);
        this.query = params;
        this.page.currentPage = 1
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        params.customerId = this.customerId;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          // this.selectionClear();
            this.selectionLoad();
        });
      }
    }
  };
</script>

<style>
</style>
